package com.kye.express.business.jd.goods_detail;

import com.afollestad.materialdialogs.MaterialDialog;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.jd.address.JDAddressBean;
import com.kye.model.bean.jd.goods_detail.JDCheckGoodsAvailableResultBean;
import com.kye.model.bean.jd.goods_detail.RequestJDCheckGoodsAvailable;
import com.kye.model.bean.jd.order_details.request.RequestGooodsExchange;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.JDShopDataSource;

import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 18/12/2017
 * @updated : 18/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public interface JDGoodsExchangeContract {

    interface JDGoodsExchangeView extends IView {

        void onLoading();

        void onLoaded();

        void onUser(User user);

        void exchangeSuccess(MaterialDialog dialog);

        void exchangeFailed(MaterialDialog dialog, String msg);
    }

    class JDGoodsExchangePresenter extends BasePresenter<JDGoodsExchangeView> {

        private JDShopDataSource jdShopDataSource;

        public JDGoodsExchangePresenter() {
            jdShopDataSource = new JDShopDataSource();
        }

        @Override
        public void bind(JDGoodsExchangeView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user);
                        }
                    });
        }

        /**
         * 检查配送地址是否有货
         * todo : 当前不进行请求处理
         * @param userInfo
         * @param item
         * @param sku
         */
        @Deprecated
        public void checkGoodsAvailableByAddress(User userInfo, JDAddressBean item, String sku) {
            Single.just(new RequestJDCheckGoodsAvailable())
                    .doOnSuccess(new Consumer<RequestJDCheckGoodsAvailable>() {
                        @Override
                        public void accept(RequestJDCheckGoodsAvailable requestJDCheckGoodsAvailable) throws Exception {
                            String address = item.getProvince() + "_" + item.getCity() + "_" + item.getCountry();
                            requestJDCheckGoodsAvailable.setAddress(address);
                            requestJDCheckGoodsAvailable.setPhone(userInfo.getPhone());
                            requestJDCheckGoodsAvailable.setSkuId(sku);
                        }
                    })
                    .flatMap(new Function<RequestJDCheckGoodsAvailable, SingleSource<OpenApiResult<JDCheckGoodsAvailableResultBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<JDCheckGoodsAvailableResultBean>> apply(RequestJDCheckGoodsAvailable requestJDCheckGoodsAvailable) throws Exception {
                            return jdShopDataSource.checkGoodsAvailable(requestJDCheckGoodsAvailable)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<JDCheckGoodsAvailableResultBean>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<JDCheckGoodsAvailableResultBean> result, JDCheckGoodsAvailableResultBean jdCheckGoodsAvailableResultBean) {
//                            getView().checkGoodsAvailableSuccess(jdCheckGoodsAvailableResultBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<JDCheckGoodsAvailableResultBean> result, int code, String msg) {
                            /**
                             0	操作成功
                             30000	操作失败
                             30026	值不能为空
                             30022	电话不能为空
                             32832	本礼品暂时无货请兑换其他礼品
                             */
                            if (32832 == code) {
//                                getView().checkGoodsUnAvailableSuccess(result);
                            } else {
//                                getView().checkGoodsAvailableServerFailed(msg);
                            }
                        }

                        @Override
                        public void onOtherError(Throwable e) {
//                            getView().checkGoodsAvailableNetFailed(e.getMessage());
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }

        public void exchange(RequestGooodsExchange requestGooodsExchange, String smsCodeInput, MaterialDialog dialog) {
            Single.just(requestGooodsExchange)
                    .doOnSuccess(new Consumer<RequestGooodsExchange>() {
                        @Override
                        public void accept(RequestGooodsExchange requestGooodsExchange) throws Exception {
                            requestGooodsExchange.setConfirmCode(smsCodeInput);
                        }
                    })
                    .flatMap(new Function<RequestGooodsExchange, SingleSource<OpenApiResult<Object>>>() {
                        @Override
                        public SingleSource<OpenApiResult<Object>> apply(RequestGooodsExchange requestGooodsExchange) throws Exception {
                            return jdShopDataSource.applyGooodsExchange(requestGooodsExchange)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<Object>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<Object> result, Object o) {
                           getView().exchangeSuccess(dialog);
                        }

                        @Override
                        public void onApiError(OpenApiResult<Object> result, int code, String msg) {
                            getView().exchangeFailed(dialog, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().exchangeFailed(dialog, e.getMessage());
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }
    }
}
